namespace Plugins.Engine.Internal
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Reflection;
    using Plugins.Engine;
    using Plugins.Interfaces;

    [Serializable]
    internal class PluginAssembly : IPluginAssemblyInternal, IPluginAssembly
    {
        //private readonly Assembly _assembly;
        private readonly byte[] _publicKey;
        private readonly IPluginObjectActivatorInternal _loader;
        private readonly AssemblyName _name;
        private readonly Type[] _types;

        public PluginAssembly(IPluginObjectActivatorInternal loader, Assembly assembly, byte[] publicKey)
        {
            this._loader = loader;
            //this._assembly = assembly;
            this._publicKey = publicKey;
            this._name = assembly.GetName();
            this._types = assembly.GetExportedTypes();
        }

        public IEnumerable<IPlugin> GetAttributes(Type type)
        {
            foreach (IPlugin attribute in type.GetCustomAttributes(typeof(PluginAttribute), true))
            {
                yield return attribute;
            }
        }

        public override string ToString()
        {
            return string.Format(CultureInfo.InvariantCulture, "{0}", this._name);
        }

        public IPluginObjectActivatorInternal Activator
        {
            get
            {
                return this._loader;
            }
        }
        public byte[] PublicKey 
        {
            get 
            {
                return this._publicKey;
            }
        }

        public AssemblyName Identity
        {
            get
            {
                return this._name;
            }
        }

        public Type[] Types
        {
            get
            {
                return this._types;
            }
        }
    }
}

